/*********************************************************************************
 *      Copyright:  (C) 2022 znmzdx
 *                  All rights reserved.
 *
 *       Filename:  mqtt_config.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(23/05/22)
 *         Author:  Guo Wenxue <guowenxue@qmail.com>
 *      ChangeLog:  1, Release initial version on "23/05/22 12:10:43"
 *                 
 ********************************************************************************/
#include "iniparser.h"
#include "mqtt_config.h"
#include "cJSON.h"
#include <stdio.h>
#include <string.h>

/* 载入设置参数 */
/* 将ini配置文件里的信息通过相关API函数复制给字符串，然后给结构体赋值*/
int get_config(char *path,mqtt_user_data_t *mqtt)
{
    dictionary      *ini = NULL;
	/* const char * 指向常量的指针，可以不用初始化 */
    const char      *brokeraddress;
    int              brokerport;
    const char      *username;
    const char      *password;
    const char      *clientid;
	const char      *sn;
    const char      *pub_topic;
    const char      *sub_topic;
    int             Qos;


    const char      *method;
    const char      *time;
    const char      *jsonid;
    const char      *identifier;
    const char      *version;
	int              keepalive;
	int              sampletime;

    if(!path || !mqtt)
    {
        printf("invalid_argument: %s\n",__FUNCTION__);
        return -1;
    }
	/*dictionary * iniparser_load(const char * ininame) 解析dictionary对象并返回dictionary对象*/
    ini = iniparser_load(path);//dictionary指针指向ini文件，作为句柄进行以下函数操作

    if(!ini)
    {
        printf("iniparser_load failed!\n");
        return -1;
    }
//	iniparser_getstring(dictionary *d, const char *key, char *def)
//		返回dictionary对象的section:key.name对应的字符串值 
//	iniparser_getint(dictionary *d, const char *key, int notfound)
//	    返回dictionary对象的section:key.name对应的整型值

    brokeraddress = iniparser_getstring(ini,"address:brokeraddress", NULL);
    username = iniparser_getstring(ini,"user_info:user_name", NULL);
    password = iniparser_getstring(ini,"user_info:user_passwd", NULL);
    sn       = iniparser_getstring(ini,"user_info:serial_no", NULL);
    clientid = iniparser_getstring(ini,"clientId:id", NULL);
    jsonid = iniparser_getstring(ini,"aliyun_json:id", NULL);
	pub_topic= iniparser_getstring(ini,"pub_topic:topic", "null");
    method   = iniparser_getstring(ini,"aliyun_json:method", NULL);



    brokerport= iniparser_getint(ini,"address:port", 1);
    keepalive = iniparser_getint(ini,"time:keep_alive", 1);
    sampletime= iniparser_getint(ini,"time:sample_time", 1);
//  identifier = iniparser_getstring(ini,"ali_json:identifier",DEFAULT_JSONID);

	strncpy(mqtt->brokeraddress, brokeraddress, MAX_SIZE);
	strncpy(mqtt->clientid, clientid, MAX_SIZE);
	strncpy(mqtt->username, username, MAX_SIZE);
	strncpy(mqtt->passwd, password, MAX_SIZE);
	strncpy(mqtt->jsonid, jsonid, MAX_SIZE);
	strncpy(mqtt->serial_no, sn, MAX_SIZE);          
	strncpy(mqtt->method, method, MAX_SIZE);          
	strncpy(mqtt->pub_topic, pub_topic, MAX_SIZE);          


	mqtt->port        = brokerport;
	mqtt->keep_alive  = keepalive;
	mqtt->sample_time = sampletime;

	//释放内存
    iniparser_freedict(ini);
    return 0;

}

/*将结构体里的值取出来打包赋值给cjson结构体*/
int pack_json(mqtt_user_data_t *mqtt, char *msg )
{


    cJSON            *root;
    cJSON            *item;

//	char              temp[MAX_SIZE];

    item = cJSON_CreateObject();
    root = cJSON_CreateObject();//创建一个json对象作为句柄，返回一个jsons结构体指针

//	snprintf(temp, sizeof(temp), "%2.2f", mqtt->sample_temp);

    cJSON_AddItemToObject(root, "method",cJSON_CreateString(mqtt->method));
    cJSON_AddItemToObject(root, "id",cJSON_CreateString(mqtt->jsonid));
    cJSON_AddItemToObject(root, "params", item);
    cJSON_AddItemToObject(item, "CurrentTemperature",cJSON_CreateNumber((float)mqtt->sample_temp));
    cJSON_AddItemToObject(root, "Version",cJSON_CreateString("1.0.0"));

    msg = cJSON_Print(root);
    printf("%s\n",msg);

    cJSON_Delete(root);

    return 0;
}



